<template>
	<div class="modal fade" id="auditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" @click="canel"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
					<h4 class="modal-title" id="myModalLabel"> {{this.auditModal.title}}</h4>
				</div>
				<div class="modal-body">
					<div class="form-horizontal">
						<div class="form-group">
							<label class="col-sm-3 control-label">审核结果：</label>
							<div class="col-sm-9">
								<select class="form-control" v-model="params.customerStatus">
									<option v-if="auditModal.customerStatus==10" :value="auditModal.customerStatus" v-res="{code:'menu_0005_0001_0005_0001'}">审核通过</option>
									<option v-if="auditModal.customerStatus==10" :value="invalidValue" v-res="{code:'menu_0005_0001_0005_0002'}">审核拒绝</option>
									<option v-if="auditModal.customerStatus==6" :value="auditModal.customerStatus" v-res="{code:'menu_0005_0001_0006_0001'}">审核通过</option>
									<option v-if="auditModal.customerStatus==6" :value="invalidValue" v-res="{code:'menu_0005_0001_0006_0002'}">审核拒绝</option>
								</select>
							</div>
						</div>
						<div class="form-group" v-if="params.customerStatus==6" v-res="{code:'menu_0005_0001_0006_0003'}">
							<label class="col-sm-3 control-label">实际到访时间：</label>
							<div class="col-sm-4">
								<DatePicker type="datetime" :disabled="editVisitTime" placeholder="实际到访时间" v-model="params.createTime" style="width: 200px"></DatePicker>
							</div>
							<div class="col-sm-4">
								<button class="btn pull-right o-color saveAndNext" @click="editVisitTime = false">修改实际到访时间</button>
							</div>
						</div>
						<div class="form-group" v-if="params.customerStatus==6 && checkMobileShow" v-res="{code:'menu_0005_0001_0006_0003'}">
							<label class="col-sm-3 control-label">短信到访验真：</label>
							<div class="col-sm-4">
								<checkMobileComp v-on:checkMobileListener='checkMobileReturn'></checkMobileComp>
							</div>
						</div>

						<div class="form-group" v-if="adviserList.length > 0">
							<label class="col-sm-3 control-label">置业顾问：</label>
							<div class="col-sm-9">
								<select class="form-control" v-model="params.adviserUuid">
									<option v-for="adviser in adviserList" :value="adviser.adviserUuid" :key="adviser.adviserUuid">{{ adviser.name }}</option>
								</select>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">备注信息：</label>
							<div class="col-sm-9">
								<textarea rows="5" class="form-control" v-model="params.remark" placeholder="请填写备注信息"></textarea>
							</div>
						</div>
						<div class="form-group">
							<label for="" class="col-sm-3 control-label">上传图片：</label>
							<div class="col-sm-9">
								<uploadFile v-bind:uploadState="fileFlag" v-on:fileList='handleAge' :key="auditModal.id"></uploadFile>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn checkedBtn" @click="save">确定</button>
					<button type="button" class="btn btn-default" @click="canel">取消</button>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import uploadFile from "../uploadFile/uploadFile.vue";
import { API, Log, Utils, Service } from "../../js/base";
import checkMobileComp from "./components/checkMobileComp.vue";
export default {
  //接收父组件的绑定值
  props: ["auditModal"],
  data() {
    return {
      //机构信息对象
      params: {
        id: "",
        customerStatus: "",
        remark: "",
        createTime: new Date(),
        adviserUuid: "",
      },
      imgesList: [],
      fileFlag: true,
      editVisitTime: true,
      invalidValue: "-1",
      adviserList: [],
      checkMobileProps: {
        id: "",
        type: "audit",
      },
      mobileChecked: false,
      checkMobileShow: false,
    };
  },
  watch: {
    auditModal: {
      handler: function(val, oldVal) {
        this.checkMobileProps.id = this.auditModal.id;
        if (
          this.auditModal.checkSwitch == "1" &&
          !this.auditModal.lastCheckTime
        ) {
          this.checkMobileShow = true;
        } else {
          this.checkMobileShow = false;
        }
      },
      deep: true,
    },
  },
  components: {
    uploadFile,
    checkMobileComp,
  },
  methods: {
    save: function() {
      const _self = this;
      // 若未点击修改实际到访时间则以保存审核结果的时间为准
      if (_self.editVisitTime) {
        _self.params.createTime = new Date();
      }
      if (_self.auditModal.customerStatus != 6) {
        _self.params.createTime = "";
      }

      if (!_self.params.customerStatus) {
        layer.msg("请选择审核结果", { icon: 7, time: 2000 });
        return false;
      }
      if (
        _self.checkMobileShow &&
        !_self.mobileChecked &&
        _self.params.customerStatus == 6
      ) {
        layer.msg("请先进行短信验真!", { icon: 7, time: 2000 });
        return false;
      }
      const url = _self.utilHelper.apiUrl + "/api/auditCP";
      const body = {};
      _self.params.id = _self.auditModal.id;
      body.params = _self.params;
      body.params.affixList = _self.imgesList;
      _self.mobileChecked = false;
      _self.$http.post(url, body).then(
        (response) => {
          const res = response.body.resData;
          if (res.result == "1") {
            layer.msg("审核成功");
            $("#auditModal").modal("hide");
            _self.fileFlag = false;
            _self.imgesList = [];
            _self.$emit("saveAuditListener");
          } else {
            layer.msg("审核失败: " + res.resultMsg, { icon: 7, time: 5000 });
          }
        },
        (response) => {
          layer.msg("系统异常");
        }
      );
    },
    handleAge(data) {
      if (data == "1") {
        this.fileFlag = true;
      } else {
        this.imgesList = data;
      }
    },
    canel: function() {
      $("#auditModal").modal("hide");
      this.fileFlag = false;
    },
    init: function() {
      this.params = {
        id: "",
        remark: "",
        customerStatus: "",
        createTime: new Date(),
        adviserUuid: "",
      };
      if (this.auditModal.customerStatus == 6) {
        this.params.createTime = new Date();
      }
      this.editVisitTime = true;
      this.getAdviserList();
    },
    getAdviserList() {
      const _this = this;
      let id = _this.auditModal.premisesId;
      _this.adviserList = [];
      if (!id) return;
      Service.Adviser.getAdviserList(id, _this)
        .then((list) => {
          if (list && list.length) {
            _this.adviserList = list;
          }
        })
        .catch((e) => {});
    },
    checkMobileReturn(msg) {
      this.mobileChecked = true;
    },
  },
};
</script>